/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    typedef struct ListNode Node;
    ListNode* removeElements(ListNode* head, int val) {
        while (NULL != head && head->val == val)
        {
            head = head->next;
        }

        Node* cur = head;
        Node* pre = head;
        while (cur != NULL)
        {
            if (cur->val != val)
            {
                pre = cur;
                cur = cur->next;
            }
            else
            {
                pre->next = cur->next;
                cur = cur->next;
            }
        }
        return head;
    }
};